package com.authManager.server.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.validation.annotation.Validated;
import org.apache.commons.lang3.StringUtils;

import com.authManager.entity.SysUser;
import com.authManager.server.service.ISysUserService;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;

import com.commons.base.model.AdminPage;
import com.commons.base.model.ResultCode;
import com.commons.base.model.ResultData;

import java.util.HashMap;
import java.util.Map;
import java.util.Date;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.validation.constraints.NotEmpty;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.Pattern;
import com.baomidou.mybatisplus.mapper.EntityWrapper;

/**
 * <p>
 * 【用户信息】 前端控制器
 * </p>
 *
 * @author 小旋风
 * @since 2018-8-15
 */

@Api(value = "/admin/sysUser", tags = "【用户信息】操作")
@RestController
@Validated
@RequestMapping(value = "/admin/sysUser", produces = BaseController.defProduces)
public class SysUserController extends BaseController {
	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(SysUserController.class);
	/*
	 * 【1】获取单个对象的方法用 get 做前缀。 【2】获取多个对象的方法用 list 做前缀。 【3】获取统计值的方法用 count 做nz前缀。
	 * 【4】插入的方法用 save（ 推荐 ） 或 insert 做前缀。 【5】删除的方法用 remove（ 推荐 ） 或 delete 做前缀。
	 * 【6】修改的方法用 update 做前缀。
	 */

	@Autowired
	ISysUserService sysUserService;

	@ApiOperation(value = "添加【用户信息】")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "name", value = "用户名", required = true),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "phone", value = "手机号", required = true), })
	@RequestMapping(value = "saveSysUser", method = { RequestMethod.POST })
	public ResultData<Object> saveSysUser(
			@RequestParam(required = true) @NotEmpty(message = "参数[name]不能为空") @Length(min = 0, max = 120, message = "参数[name]最大长度不能超过{max}") String name,
			@RequestParam(required = true) @NotEmpty(message = "参数[phone]不能为空") @Pattern(regexp = "^[1]{1}[0-9]{10}$", message = "手机号格式不正确") String phone) {
		SysUser sysUser = new SysUser();
		if (StringUtils.isNotBlank(name)) {
			sysUser.setName(name);
		}
		if (StringUtils.isNotBlank(phone)) {
			SysUser sysUserTmp = sysUserService.selectOne(new EntityWrapper<SysUser>(new SysUser().setPhone(phone)));
			if (sysUserTmp != null) {
				return new ResultData.Builder<Object>(ResultCode.verifyUniqueness)
						.setMessage("参数：【phone】的值：【" + phone + "】已经存在").build();
			}
			sysUser.setPhone(phone);
		}
		sysUser.setCreater(getCurrUserLoginId());
		sysUser.setCreateTime(new Date());
		sysUser.setUpdater(null);
		sysUser.setUpdateTime(null);
		sysUserService.insert(sysUser);
		Object sysUserId = sysUser.getSysUserId();
		return new ResultData.Builder<Object>(ResultCode.success).setData(sysUserId).build();
	}

	@ApiOperation(value = "修改【用户信息】")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "sysUserIdStr", value = "主键id   改成字符串类型", required = true),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "name", value = "用户名", required = true),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "phone", value = "手机号", required = true), })
	@RequestMapping(value = "editSysUser", method = { RequestMethod.POST })
	public ResultData<Object> editSysUser(@RequestParam(required = true) String sysUserId,
			@RequestParam(required = true) @NotEmpty(message = "参数[name]不能为空") @Length(min = 0, max = 120, message = "参数[name]最大长度不能超过{max}") String name,
			@RequestParam(required = true) @NotEmpty(message = "参数[phone]不能为空") @Pattern(regexp = "^[1]{1}[0-9]{10}$", message = "手机号格式不正确") String phone) {
		SysUser sysUser = sysUserService.selectById(sysUserId);
		if (sysUser == null) {
			return new ResultData.Builder<Object>(ResultCode.isNull).build();
		}
		sysUser = new SysUser();
		if (StringUtils.isNotBlank(sysUserId)) {
			SysUser sysUserTmp = sysUserService
					.selectOne(new EntityWrapper<SysUser>(new SysUser().setSysUserId(sysUserId)));
			if (sysUserTmp != null && sysUserTmp.getSysUserId() != sysUserId) {
				return new ResultData.Builder<Object>(ResultCode.verifyUniqueness)
						.setMessage("参数：【sysUserId】的值：【" + sysUserId + "】已经存在").build();
			}
			sysUser.setSysUserId(sysUserId);
		}
		if (StringUtils.isNotBlank(name)) {
			sysUser.setName(name);
		}
		if (StringUtils.isNotBlank(phone)) {
			SysUser sysUserTmp = sysUserService.selectOne(new EntityWrapper<SysUser>(new SysUser().setPhone(phone)));
			if (sysUserTmp != null && sysUserTmp.getSysUserId() != sysUserId) {
				return new ResultData.Builder<Object>(ResultCode.verifyUniqueness)
						.setMessage("参数：【phone】的值：【" + phone + "】已经存在").build();
			}
			sysUser.setPhone(phone);
		}
		// sysUser.setCreater(null);
		// sysUser.setCreateTime(null);
		sysUser.setUpdater(getCurrUserLoginId());
		sysUser.setUpdateTime(new Date());
		sysUserService.updateById(sysUser);
		return new ResultData.Builder<Object>(ResultCode.success).build();
	}

	@ApiOperation(value = "删除【用户信息】")
	@RequestMapping(value = "delSysUser", method = { RequestMethod.POST })
	public ResultData<Object> delSysUser(@RequestParam(required = true) Long sysUserId) {
		SysUser sysUser = sysUserService.selectById(sysUserId);
		if (sysUser == null) {
			return new ResultData.Builder<Object>(ResultCode.isNull).build();
		}
		// 自己根据需求放开注释吧
		// 物理删除
		// sysUserService.deleteById(sysUserId);
		// 逻辑删除
		// sysUser.setStatus(0);
		// sysUserService.updateById(sysUserId);
		return new ResultData.Builder<Object>(ResultCode.success).build();
	}

	@ApiOperation(value = "获取【用户信息】")
	@RequestMapping(value = "getSysUser", method = { RequestMethod.POST })
	public ResultData<SysUser> getSysUser(@RequestParam(required = true) Long sysUserId) {
		SysUser sysUser = sysUserService.selectById(sysUserId);
		if (sysUser == null) {
			return new ResultData.Builder<SysUser>(ResultCode.isNull).build();
		}
		return new ResultData.Builder<SysUser>(ResultCode.success).setData(sysUser).build();
	}

	@ApiOperation(value = "列表【用户信息】")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageNum", value = "页码", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageSize", value = "每页大小", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "sysUserId", value = "主键id   改成字符串类型", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "name", value = "用户名", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "phone", value = "手机号", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "start_createTime", value = "开始-创建日期", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "end_createTime", value = "截至-创建日期", required = false), })
	@RequestMapping(value = "listSysUser", method = { RequestMethod.POST })
	public ResultData<AdminPage<SysUser>> listSysUser(
			@RequestParam(defaultValue = "1", required = false) Integer pageNum,
			@RequestParam(defaultValue = "10", required = false) Integer pageSize,
			@RequestParam(required = false) String sysUserId, @RequestParam(required = false) String name,
			@RequestParam(required = false) String phone, @RequestParam(required = false) String start_creater,
			@RequestParam(required = false) String end_creater) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("pageNum", pageNum);
		paramMap.put("pageSize", pageSize);
		System.out.println(paramMap);
		// 主键id 改成字符串类型
		if (StringUtils.isNotBlank(sysUserId)) {
			paramMap.put("sysUserId", sysUserId);
		}
		// 用户名
		if (StringUtils.isNotBlank(name)) {
			paramMap.put("name", name);
		}
		// 手机号
		if (StringUtils.isNotBlank(phone)) {
			paramMap.put("phone", phone);
		}
		// 开始-创建人
		if (StringUtils.isNotBlank(start_creater)) {
			paramMap.put("start_creater", start_creater);
		}
		// 结束-创建人
		if (StringUtils.isNotBlank(end_creater)) {
			paramMap.put("end_creater", end_creater);
		}
		return sysUserService.listSysUser(paramMap);
	}
}
